package com.wj.service;


import com.wj.mapper.UsersMapper;
import com.wj.pro.Users;
import com.wj.until.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UsersServiceImp implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public JsonData allUsers() {
        List<Users> all = usersMapper.findAll();
        return JsonData.buildSuc(all);
    }

    @Override
    public JsonData deletes(int id) {
        int result = usersMapper.deletes(id);
        if (result >= 0) {
            return JsonData.buildSuc("删除成功");
        }
        return JsonData.buildError("删除失败");
    }

    @Override
    public JsonData getById(int id) {
        Users users = usersMapper.getById(id);
        return JsonData.buildSuc(users);
    }

    @Override
    public JsonData add(Users users) {
        int result = 0;
        if (users.getId() == null) {
            result = usersMapper.add(users);
        } else {
            result = usersMapper.edit(users);
        }
        if (result > 0) {
            JsonData.buildError("编辑失败");
        }
        return JsonData.buildSuc("编辑成功");
    }

}
